<?php
 
namespace app\index\service;

use think\Model;
use app\index\model\StudentCourse;
use app\index\model\Student as StudentModel;
use app\index\model\Appointment;
use app\index\model\StudentClbum;

class Student extends Model
{
    /**
     * 用户登录
     * @date: 2017年8月11日 下午11:07:16
     * @author: hxzlh
     * @param unknown $phone
     * @param unknown $openid
     * @return: return
     */
    public function login($phone, $openid) 
    {
        //查询用户信息
        $student = StudentModel::get(['phone'=>$phone]);
        if (empty($student)) {
            throw new \think\Exception('用户未添加,请联系管理员');
        }
        $oldStudent = StudentModel::get(['openid'=>$openid]);
        if ($oldStudent && $oldStudent['id'] != $student['id']) {
            $oldStudent->openid = '';
            $oldStudent->save();
        }
        if (empty($student['openid']) || $student['openid'] != $openid) {
            $student->openid = $openid;
            $student->save();
        }
        $data['phone'] = $phone;
        $data['openid'] = $openid;
        $data['id'] = $student['id'];
        $data['name'] = $student['name'];
        session('user_login', $data);
        session('user_login_sign', data_auth_sign($data));
        return $data;
    }
    
    /**
     * 获取用户课程列表
     * @date: 2017年8月12日 上午12:10:40
     * @author: hxzlh
     * @param unknown $student_id
     * @return unknown
     * @return: return
     */
    public function getCourse($student_id) 
    {
        $model = new StudentCourse();
        $where['student_id'] = $student_id;
        $field = ['name','teacher','remain_hours'];
        $data = $model
        ->field($field)
        ->join('elite_course','elite_student_course.course_id = elite_course.id','LEFT')
        ->where($where)
        ->select();
        return $data;
    }
    
    public function getClbum($student_id) 
    {
        $model = new StudentClbum();
        $where['student_id'] = $student_id;
        $field = ['elite_clbum.name'=>'clbum_name','elite_course.name'=>'course_name','day_week','start_time','end_time'];
        $data = $model
        ->field($field)
        ->join('elite_clbum','elite_student_clbum.clbum_id = elite_clbum.id','LEFT')
        ->join('elite_course','elite_clbum.course_id = elite_course.id','LEFT')
        ->where($where)
        ->select();
        return $data;
    }
    /**
     * 用户预约
     * @date: 2017年8月12日 下午1:27:12
     * @author: hxzlh
     * @param unknown $name
     * @param unknown $phone
     * @param unknown $openid
     * @param unknown $course
     * @param number $level
     * @throws \think\Exception
     * @return boolean
     */
    public function appointment($name, $phone, $course, $gender=0, $openid='', $level=0, $comment='') 
    {
        $data['name'] = $name;
        $data['phone'] = $phone;
        $data['openid'] = $openid;
        $data['course_id'] = $course;
        $data['gender'] = $gender;
        $data['level'] = $level;
        $data['comment'] = $comment;
        $res = Appointment::create($data);
        if (!$res) {
            throw new \think\Exception('预约失败,请重新预约');
        }
        return $res;
    }
    
}